package com.supermap.disaster.service.impl;

import com.supermap.common.JSONUtil;
import com.supermap.disaster.dao.DisasterTypeDao;
import com.supermap.disaster.entity.DisasterComment;
import com.supermap.disaster.entity.DisasterType;
import com.supermap.disaster.entity.Disasterpraise;
import com.supermap.disaster.service.DisasterTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxs
 * @Title:
 * @Description:
 * @date 2018/3/1  15:23
 */
@Service
public class DisasterTypeServiceImpl implements DisasterTypeService {

    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(DisasterTypeServiceImpl.class);

    @Resource
    private DisasterTypeDao disasterTypeDao;
    @Override
    public JSONUtil getAllDisasterType() {//查询全部灾害信息
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try {
            List<DisasterType> disasterTypes = disasterTypeDao.getAllDisasterType();
            jsonUtil.setMsg("Get attens success！");
            jsonUtil.setIsSuccess(true);
            jsonUtil.setCode(200);
            jsonUtil.setData(disasterTypes);
        } catch (Exception e) {
            //捕捉数据库异常
            logger.error("DisasterTypeServiceImpl=>getDisasterType=>DaoException:" + e.getStackTrace());
            jsonUtil.setMsg("Get attens flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }

    @Override
    public int getDisasterPraiseCount(int disasterId) {
        return disasterTypeDao.getDisasterPraiseCount(disasterId);
    }

    @Override
    public boolean getDisasterPraise(int userId, int disasterId) {
       String  disasterPraise=disasterTypeDao.getDisasterPraise(userId,disasterId);
       if(disasterPraise==null){
           return true;
       }
        return false;
    }

    @Override
    public boolean addDisasterPraise(Disasterpraise disasterpraise) {
        String disasterPraise=disasterTypeDao.getDisasterPraise(disasterpraise.getUserId(),disasterpraise.getDisasterId());
        if(disasterPraise==null){
            int d=disasterTypeDao.addDisasterPraise(disasterpraise);
            if(d==1){
                return  true;
            }
            return false;
        }else{
            int d2=disasterTypeDao.DelDisById(disasterpraise.getUserId(),disasterpraise.getDisasterId());
            return false;
        }
    }

    @Override
    public boolean addDisasterComment(DisasterComment disasterComment,String uid) {
        int d=disasterTypeDao.addDisasterComment(disasterComment);
        if(d==1){
            disasterTypeDao.addNewMessage(disasterComment.getUserId(),disasterComment.getDisasterId(),3,disasterComment.getComment(),0,uid);
            return true;
        }
        return false;
    }

    @Override
    public JSONUtil getAllDisasterCommentByagrInfoId(int disasterId) {
        List<DisasterComment> disasterComments=new ArrayList<DisasterComment>();
        List<DisasterComment> list=disasterTypeDao.getAllDisasterCommentByagrInfoId(disasterId);
        //初始化Json封装工具类
        JSONUtil jsonUtil = new JSONUtil();
        try{
            for(DisasterComment d:list){
                if(d.getParentId()==-1)disasterComments.add(d);
            }
            getDisasterComment(list, disasterComments);
           int disastercount= list.size();
           jsonUtil.setMsg("DisasterCommentCount:"+disastercount);
           jsonUtil.setIsSuccess(true);
           jsonUtil.setCode(200);
           jsonUtil.setData(disasterComments);
        }catch (Exception e){
            logger.error("DisasterCommentServiceImpl=>getAllDisasterComments=>DaoException:"+e.getMessage());
            jsonUtil.setMsg("get getAllDisasterComments flase！");
            jsonUtil.setIsSuccess(false);
            jsonUtil.setCode(100);
        }
        return jsonUtil;
    }
    //递归(根据ParentId和对应的ID得到List)
    private void getDisasterComment(List<DisasterComment> list, List<DisasterComment> disasterComments) {
        if(disasterComments.size()!=0) {
            for(DisasterComment d:disasterComments){
                List<DisasterComment> dclist=new ArrayList<>();
                for (DisasterComment d2 : list) {
                    if (d2.getParentId() == d.getId())dclist.add(d2);
                }
                getDisasterComment(list,dclist);
                d.setChildrenNode(dclist);
            }
        }
    }
}
